import timeit

import sys
import os

from func_moead import Params, Problem, init_subproblem, terminate, genetic_op, update_vec, calculateigd
import numpy as np
import tool

insname = 'DTLZ3'
dim = 19
nobj = 10
popsize = 275
T = 20
stop_nfeval = 300000
######################

params = Params(popsize,T,'tc',2500,stop_nfeval,0.9,2,0.5,1)
mop = Problem(insname,dim,nobj)

subproblems, idealpoint = init_subproblem(mop,params)

start = timeit.default_timer()

itr = 0
evalcounter = 0
while not terminate(evalcounter,params):

    for i in range(params.popsize):

        #decide on whether choosing parents from the neighbourhood or not
        updateneighbour = np.random.rand(1)[0] < params.updateprob

        #generate new solution
        newpoint = genetic_op(i,updateneighbour,mop,
                              params,subproblems,'current')

        #evaluate new solution
        newpoint.value = mop.evaluate(newpoint.parameter)
        evalcounter = evalcounter + 1

        #update estimated idealpoint
        idealpoint = np.minimum(idealpoint,newpoint.value)

        #update current population
        update_vec(i,updateneighbour,newpoint,mop,
                   params,subproblems,idealpoint)
    itr += 1
    print itr

#calculate and display igd value
pf = np.array([subproblems[i].curpoint.value for i in range(params.popsize)])
stop = timeit.default_timer()
#print running time
print 'running time %f s'%(stop-start)
path = os.path.abspath(os.path.dirname(sys.argv[0]))
truepf = np.loadtxt(path + "/PF/%s.dat"%mop.name)
# print 'igd: %f'%calculateigd(truepf, pf)
np.savetxt('data/moead_pf/%s_%dD.dat' % (insname, mop.nobj), pf, delimiter='\t')
tool.plot(data=pf)




